home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8436 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  49 lines

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Problem Negating an Unsigned Char
  5. Date: 3 Mar 1996 22:22:52 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4he27sINNdel@keats.ugrad.cs.ubc.ca>
  8. References: <Dnnros.Lq.0.-s@hkusuc.hku.hk>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <Dnnros.Lq.0.-s@hkusuc.hku.hk>,
  12. Starry Hung <h8716718@hkusua.hku.hk> wrote:
  13. >Hi,
  14. >
  15. >The code is like that:
  16. >
  17. >unsigned char a=0x11;
  18. >unsigned char b=0xEE;
  19. >int c=0;
  20. >
  21. >void main( void ) {
  22.  
  23. The declaration of main() is incorrect. It should read "int main(void)".
  24.  
  25. >    if( a == ~b ) {
  26. >      c=1;
  27. >    }
  28. >}
  29.  
  30. exit status?
  31.  
  32. >
  33. >The c remains unchange, while it changes to 1 if I cast the ~b to unsigned
  34.  
  35. How do you know??? Your program produces no output, and fails to give an exit
  36. status to the operating system.
  37.  
  38. >char as if( a == (unsigned char) ~b )
  39.  
  40. The cast forces the integer value of ~b into an unsigned char, stripping
  41. high-order bits. The above will work only on machines with eight bit chars.
  42.  
  43. To make it portable, you must manually mask for the lower eight bits:
  44.  
  45.     if (a == (~b & 0xff))
  46.  
  47. -- 
  48.  
  49.